package com.rul.mars.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.rul.mars.common.constant.DeleteStatusConstant;
import com.rul.mars.mbg.domain.CommentDetail;
import com.rul.mars.mbg.domain.CommentParam;
import com.rul.mars.mbg.domain.CommentReplayDetail;
import com.rul.mars.mbg.domain.OrderDetail;
import com.rul.mars.mbg.mapper.CommentMapper;
import com.rul.mars.mbg.mapper.CommentReplayMapper;
import com.rul.mars.mbg.model.*;
import com.rul.mars.order.service.CommentService;
import com.rul.mars.order.service.OrderService;
import com.rul.mars.order.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 评论服务实现
 *
 * @author RuL
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CommentReplayMapper commentReplayMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;


    /**
     * 删除评论
     *
     * @param id 评论ID
     * @return 删除结果
     */
    @Override
    public boolean deleteComment(Long id) {
        //查询评论
        Comment comment = commentMapper.selectByPrimaryKey(id);

        CommentReplay commentReplay = new CommentReplay();
        commentReplay.setDeleteStatus(DeleteStatusConstant.YES);

        CommentReplayExample example = new CommentReplayExample();
        example.createCriteria()
                .andCommentIdEqualTo(id);

        //修改删除状态
        comment.setDeleteStatus(DeleteStatusConstant.YES);
        int rows = commentMapper.updateByPrimaryKeySelective(comment);
        commentReplayMapper.updateByExample(commentReplay, example);

        return rows == 1;
    }

    /**
     * 创建评论
     *
     * @param param 创建评论参数
     * @return 创建后的评论详情
     */
    @Override
    public CommentDetail create(CommentParam param) {
        Comment comment = BeanUtil.copyProperties(param, Comment.class);
        User user = userService.currentUser();
        OrderDetail orderDetail = orderService.detail(comment.getOrderId());

        //设置用户信息
        comment.setUserId(user.getId());
        comment.setUserNickName(user.getNickName());
        comment.setUserIcon(user.getIcon());
        //设置商品信息
        comment.setOrderId(orderDetail.getId());

        commentMapper.insertSelective(comment);
        return BeanUtil.copyProperties(comment, CommentDetail.class);
    }

    /**
     * 根据ID查询评论详情
     *
     * @param id 评论ID
     * @return 评论详情
     */
    @Override
    public CommentDetail detail(Long id) {
        //查找评论
        Comment comment = commentMapper.selectByPrimaryKey(id);
        //查找回复的评论
        CommentReplayExample example = new CommentReplayExample();
        example.createCriteria()
                .andCommentIdEqualTo(id);
        List<CommentReplay> commentReplays = commentReplayMapper.selectByExample(example);

        //转为Detail
        CommentDetail commentDetail = BeanUtil.copyProperties(comment, CommentDetail.class);
        List<CommentReplayDetail> replayDetails = commentReplays.stream()
                .map(item -> BeanUtil.copyProperties(item, CommentReplayDetail.class))
                .collect(Collectors.toList());

        commentDetail.setReplayDetails(replayDetails);
        return commentDetail;
    }

    /**
     * 分页查询商品的评论
     *
     * @param productId 商品ID
     * @param pageNum   分页号
     * @param pageSize  分页大小
     * @return 商品评论
     */
    @Override
    public List<CommentDetail> listCommentDetail(Long productId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        CommentExample example = new CommentExample();
        example.createCriteria()
                .andProductIdEqualTo(productId);

        List<Comment> commentList = commentMapper.selectByExample(example);
        //转为detail
        return commentList.stream()
                .map(item -> BeanUtil.copyProperties(item, CommentDetail.class))
                .collect(Collectors.toList());
    }
}
